app.directive('regFormBlock', function() {
	return {
		controller: function($scope, $http, $rootScope, $compile, $timeout) {

			$scope.innerCheck = function() {
				var result;
				if(!$scope.l.isrange) {
					if($scope.model.tmp == '') {
						layer.open({
							content: '请填写完整'
						})
						return false;
					} else if($scope.model.tmp) {
						result = $scope.model.tmp;
					}

				} else if($scope.l.isrange) {
					if($scope.model.minTmp == '' || $scope.model.maxTmp == '') {
						layer.open({
							content: '请填写完整'
						})
						return false;
					} else if($scope.model.minTmp && $scope.model.maxTmp) {
						result = $scope.model.minTmp + '~' + $scope.model.maxTmp;
					}
				}
				$rootScope.closeDialog();
				return {
					isrange: $scope.l.isrange,
					min: $scope.model.minTmp,
					max: $scope.model.maxTmp,
					result: result
				}
			}

			$scope.putData = function() {
				var d = new $scope.innerCheck();

				if(!d.isrange && d) {
					$scope.l.value = d.result;
				} else if(d.isrange && d) {
					$scope.l.data.min = d.min;
					$scope.l.data.max = d.max;
				}
			}

			$scope.buildDialog = function() {
				var placeholder = '',
					placeholder1 = '',
					placeholder2 = '',
					_optionRange='';
				$scope.model = {
					_optionRange:'',
					tmp: $scope.l.value,
					minTmp: ($scope.l.data?$scope.l.data.min:'f'),
					maxTmp: ($scope.l.data?$scope.l.data.max:'f')
				}
				if($scope.l.type == 'input') {
					if($scope.l.id == 'realname') {
						placeholder = '请输入真实姓名'
					} else if($scope.l.id == 'phone') {
						placeholder = '请输入手机号码'
					} else if($scope.l.id == 'weixin') {
						placeholder = '请输入微信号'
					} else if($scope.l.id == 'height') {
						if($scope.l.isrange) {
							placeholder1 = '请输入最低身高CM'
							placeholder2 = '请输入最大身高CM'
						} else {
							placeholder = '请输入身高CM'
						}
						name='身高';
						$scope.model._optionRange=(function(){
							var _option=new Array();
							for(var i=_heightStart;i<=_heightEnd;i++)
							{
								var tmp=new Object();
								if(i==_heightStart)
								{
									tmp.name='不限';
									tmp.id='f';
								}
								else
								{
									tmp.name=i+'CM';
									tmp.id=i;
								}
								_option.push(tmp)
							}
							return _option;
						})()
					} else if($scope.l.id == 'weight') {
						if($scope.l.isrange) {
							placeholder1 = '请输入最小体重KG'
							placeholder2 = '请输入最大体重KG'
						} else {
							placeholder = '请输入体重KG'
						}
						name='体重';
						$scope.model._optionRange=(function(){
							var _option=new Array();
							for(var i=_weightStart;i<=_weightEnd;i++)
							{
								var tmp=new Object();
								if(i==_weightStart)
								{
									tmp.name='不限';
									tmp.id='f';
								}
								else
								{
									tmp.name=i+'KG';
									tmp.id=i;
								}
								_option.push(tmp)
							}
							return _option;
						})()
					} else if($scope.l.id == 'earning') {
						if($scope.l.isrange) {
							placeholder1 = '请输入最低年收入(万)'
							placeholder2 = '请输入最高年收入(万)'
						} else {
							placeholder = '请输入年收入(万)'
						}
						name='年收入';
						$scope.model._optionRange=(function(){
							var _option=new Array();
							for(var i=_earningStart;i<=_earningEnd;i++)
							{
								var tmp=new Object();
								if(i==_earningStart)
								{
									tmp.name='不限';
									tmp.id='f';
								}
								else
								{
									tmp.name=i+'万';
									tmp.id=i;
								}
								_option.push(tmp)
							}
							return _option;
						})()
					} else if($scope.l.id == 'register') {
						placeholder = '请输入户口所在地'
					} else if($scope.l.id == 'age') {
						if($scope.l.isrange) {
							placeholder1 = '请输入最小年龄'
							placeholder2 = '请输入最大年龄'
						} else {
							placeholder = '请输入年龄'
						}
						name='年龄';
						$scope.model._optionRange=(function(){
							var _option=new Array();
							for(var i=_ageStart;i<=_ageEnd;i++)
							{
								var tmp=new Object();
								if(i==_ageStart)
								{
									tmp.name='不限';
									tmp.id='f';
								}
								else
								{
									tmp.name=i+'岁';
									tmp.id=i;
								}
								_option.push(tmp)
							}
							return _option;
						})()
					}
					$rootScope.dialogUi.isShow = true;
					$rootScope.dialogUi.id = $scope.l.id;
					$rootScope.dialogUi.title = $scope.l.name;
					var dialog = '<div id="dialogUi_mask" class="maskout">' +
						'<div class="dialog middle">' +
						'<div class="animated" style="opacity:0">' +
						'<h2>' +
						'{{l.name}}' +
						'<a ng-click="closeDialog()" class="dia-close">' +
						'<i class="iconfont">&#xe603;</i>' +
						'</a>' +
						'</h2>' +
						'<input ng-if="!l.isrange" autofocus="autofocus"  ng-model="model.tmp"  type="text" placeholder="' + placeholder + '"/>' +
//						'<input ng-if="l.isrange" autofocus="autofocus"   ng-model="model.minTmp" type="tel" placeholder="' + placeholder1 + '"/>' +
//						'<input ng-if="l.isrange" ng-model="model.maxTmp"  type="tel" placeholder="' + placeholder2 + '"/>' +
						'<div ng-if="l.isrange">'+
							'<span>最小'+name+'</span>'+
							'<select ng-model="model.minTmp">'+
								'<option ng-repeat="o in model._optionRange" value="{{o.id}}">{{o.name}}</option>'+
							'</select>'+
						'</div>'+
						'<div ng-if="l.isrange">'+
							'<span>最大'+name+'</span>'+
							'<select ng-model="model.maxTmp">'+
								'<option ng-repeat="o in model._optionRange" value="{{o.id}}">{{o.name}}</option>'+
							'</select>'+
						'</div>'+
						'<a ng-click="putData()" class="submit">确定</a>' +
						'</div>' +
						'</div>' +
						'<div class="mask" ng-click="closeDialog()"></div>' +
						'</div>';
					if($scope.l.value && !$scope.l.isrange) {
						$scope.model.tmp = $scope.l.value;
					} else if($scope.l.value && $scope.l.isrange) {
						var str = $scope.l.value.split('~');
						$scope.model.minTmp = str[0];
						$scope.model.maxTmp = str[1];
					}
					var el = $compile(dialog)($scope);
					angular.element(document.body).append(el);
					var a = angular.element(document.getElementById('dialogUi_mask')).children().children();
					$timeout(function() {
						a.addClass('hndialog');
					}, 100)
				}

			}
		},
		link: function(s, e, a, c) {

		}
	}
})

app.directive('culWidth', function() {
	return {
		controller: function($scope, $http ,$rootScope) {
			this.culwidth = function(type) {
				var len;
				if(type == 'value') {
					var len = $scope.l[type].length;
				} else if(type=='name'){
					for(var i = 0; i < $scope.l.option.length; i++) {
						if($scope.l.value == $scope.l.option[i].id) {
							var name = $scope.l.option[i].name.replace(/[^\u4e00-\u9fa5]/gi, '');
							var len = name.length;
						}
					}
				} else {
					for(var i = 0; i < $scope.l.option.length; i++) {
						if($scope.l.value == $scope.l.option[i].id) {
							var name = $scope.l.option[i].name;
							var len = name.length;
						}
					}
				}
				
				return len;
			}
			
			this.culWidthByElement=function(type){
				var culWidthByElement=function(text){
					var p='<select id="hidden_culWidth">'
						  	+'<option value="0">'+text+'</option>'
						  +'</select>';
					angular.element(document.body).append(p);
					var u = parseUA();
					if(u.android)
					{
						angular.element(document.getElementById('hidden_culWidth')).css({
							'padding-right':'2rem'
						})
					}
					var w=angular.element(document.getElementById('hidden_culWidth'))[0].offsetWidth;
					angular.element(document.getElementById('hidden_culWidth')).remove();
					console.log(w);
				    return w;
				}
				var _TEXT;
				if(type == 'value') {
					_TEXT = $scope.l[type];
				} else {
					for(var i = 0; i < $scope.l.option.length; i++) {
						if($scope.l.value == $scope.l.option[i].id) {
							_TEXT = $scope.l.option[i].name;
						}
					}
				}
				return culWidthByElement(_TEXT);
			}
		},
		link: function(s, e, a, c) {

			s.$watch('l.value', function() {
				
				var checkId=function(id){
					var checkArr=[
						'province',
						'city',
						'nation',
						'registerProvince',
						'registerCity',
						'ccity',
						'cprovince'
						]
					
					for(var i=0;i<checkArr.length;i++)
					{
						if(id==checkArr[i])
						{
							return id;
						}
					}
					return false;
				}
				
				var andrhack = 0,
					_andrhack = 0,
					u = parseUA();
				if(u.android) {
					_andrhack = 0.8;
					andrhack = 0.5;
				}
				if(checkId(s.l.id)) {
					var l = c.culwidth('value');
					var w=c.culWidthByElement('value');
				} else {
					var l = c.culwidth('name');
					var w=c.culWidthByElement('name');
				}
				
				e.css({
					"width":w
				})
				
//				if(s.l.id == 'job') {
//					andrhack = andrhack + _andrhack;
//					e.css({
//						"width": (parseFloat(1.08 * l) + andrhack) + 'rem'
//					})
//				} 
//				else if(s.l.standard)
//				{
//					e.css({
//						"width": (parseFloat(0.7 * l) + andrhack) + 'rem'
//					})
//				}
//				else {
//					e.css({
//						"width": (parseFloat(l) + andrhack) + 'rem'
//					})
//				}

			})

		}
	}
})

app.directive('wxUpload', function() {
	return {
		controller: function($scope, $http) {

			$scope.upload = function(obj, type , iswork) {
				if(iswork)
				{
//								var url='http:\/\/oss.mboxone.com\/20160919\/upload\/E8YykE3p7pkPk7DbfMYk1j010z3JQkM6BRK2My5TDuxY4fIOW72vm2Lo5NR0EBMi.jpg';
//								if(type=='object')
//								{
//									obj.value=url
//								}
//								else if(type=='array')
//								{
//									var len=obj.length;
//									obj.splice(len-1, 0, {url:url});
//									
//									var photos=new Array();
//									photos.push(url);
//									
//									var _u=buildUrl('json',"hn",'setPhoto',photos);
//									$http.get(_u).success(function(data){
//										console.log(data);
//									})
//				
//								}

					wx_share.uploadMultImage(function(data) {
						$scope.$apply(function() {
							var url = data.path;
							if(type == 'object') {
								obj.value = url
							} else if(type == 'array') {
	
								var photos = new Array();
								photos.push(url);
	
								var _u = buildUrl('json', "hn", 'setPhoto', photos);
								$http.get(_u).success(function(data) {
									if(data.code == 0) {
										var len = obj.length;
										obj.splice(len - 1, 0, {
											url:url
										});
									} else {
										$.util.alert(data.msg)
									}
	
								})
	
							}
						})
					});

				}

			}

		}
	}
})
app.directive('choseAgent', function() {
	return {
		controller: function($http, $scope) {
			$scope.choseAgent = function() {
				for(var i = 0; i < $scope.agentData.agentlist.length; i++) {
					if($scope.agentData.agentlist[i].$$hashKey != $scope.al.$$hashKey) {
						$scope.agentData.agentlist[i].selected = false;
					}
				}
				if($scope.al.selected) {
//					$scope.al.selected = false;
				} else {
					$scope.al.selected = true;
					$scope.agentData.agentId = $scope.al.id;
				}
			}
		}
	}
})

app.directive('hnBottomBtn', function() {
	return {
		restrict: 'C',
		controller: function($scope) {
			$scope.common = {
				resUrl: resUrl,
				version: version
			}

			$scope.clickbtn = function(self) {
				//				if(self.active)
				//				{
				//					self.active=false;
				//				}
				//				else
				//				{
				//					self.active=true;
				//				}
			}
		},
		link: function(s, e, a, c) {

		}
	}
})

//swiper 插件 
app.directive('swiperDirective', ["$rootScope", function($rootScope) {
	return {
		controller: function($timeout, $scope) {
			$scope.swindex=1;
			var me = this;
			this.isready = false;
			this.ready = function() {
				var swcallBackfuns=new Object();
				swcallBackfuns._onTransitionEnd=function(swiper){
					$scope.$apply(function(){
						$scope.swindex=(swiper.activeIndex+1);
					})
				}
				$rootScope.initSwiper('.swiper-container',swcallBackfuns);
				me.isready = true;
			}
			
		},
		link: function(s, e, a, c) {}
	}
}]);
app.directive('swiperSlide', function() {
	return {
		restrict: "A",
		require: "^swiperDirective",
		link: function(s, e, a, c) {

			if(s.$last && !c.isready) {
				c.ready();
				console.log('开始渲染swiper插件');
			}
		}
	}
})

app.directive('pickTimer', function() {
	return {
		controller: function() {

		},
		link: function(s, e, a, c) {
			console.log(s.l.value);
			var currYear = (new Date()).getFullYear();
			var opt = {};
			opt.default = {
				preset: 'date',
				theme: 'android-ics light', //皮肤样式
				display: 'bottom', //显示方式 
				mode: 'scroller', //日期选择模式
				dateFormat: 'yyyy-mm-dd',
				lang: 'zh',
				showNow: true,
				nowText: "今天",
				startYear: currYear - 100, //开始年份
				endYear: currYear + 10 //结束年份
			};
			e.mobiscroll(opt['default']);

		}
	}
})

app.directive('remApp', function() {
	return {
		controller: function($rootScope) {
			(function(doc, win) {
				var docEl = doc.documentElement,
					resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
					recalc = function() {
						var clientWidth = docEl.clientWidth;
						if(!clientWidth) return;
						docEl.style.fontSize = 20 * (clientWidth / 530) + 'px';
						document.body.style.display = "block";
					};
				if(!doc.addEventListener) return;
				win.addEventListener(resizeEvt, recalc, false);
				recalc();
				$rootScope.uiObj.remApp = true;
			})(document, window);
		}
	}
})



//公共类

//跳转处理
app.directive('trHref',function(){
	return {
		scope:{
			trHref:'='
		},
		controller:function($scope, $http, $rootScope){

		},
		link:function(s,e,a,c){
			e.on('click',function(){
				window.location.href=s.trHref.url;
			})
		}
	}
})

//相亲状态
app.directive('loveStatus',function(){
	return {
		scope:{
			loveStatus:'='
		},
		controller:function($scope,$http,$stateParams,$rootScope){
			$scope.ui={}
			var dpa=$scope.loveStatus;
			console.log($scope);
			var loveIt=dpa.data.loveIt,
				loveMe=dpa.data.loveMe,
				handMe=dpa.data.handMe,
				handIt=dpa.data.handIt;
			
			var method=null;
			if(handIt&&handMe)
			{
				//牵手成功  
				dpa.data.btntext='牵手成功';
				method=null;
			}
			else if(handIt)
			{
				//等待牵手
				dpa.data.btntext='等待牵手';
				method=null;
			}
			else if(loveIt&&loveMe)
			{
				//互相喜欢，可以发送牵手
				dpa.data.btntext='请求牵手';
				method='doHand';
			}
			else if(loveIt)
			{
				//已关注
				dpa.data.btntext='已关注';
				method=null;
			}
			else 
			{
				dpa.data.btntext='关注';
				method='doLove';
			}
			
			//牵手
			this.dohand=function(){
				var _link=buildUrl('json','hn',method,{
					uid:$stateParams.uid
				})
				if(method)
				{
					$rootScope.uiObj.layerloadingCtrl=true;
					$http.get(_link).success(function(data){
						if(data.code==0)
						{
							location.reload();
						}
						else
						{
							if(data.code==98)
							{
								goreg('您还没有注册，是否需要前往注册页面？')
							}
							else if(data.code==97)
							{
								gorecharge('关注成功后将会花费一次配对次数，请先充值购买配对次数。')
							}
							else
							{
								$.util.alert(data.msg)
							}
						}
						$rootScope.callSuccessHttp();
					})
				}	
			}
			
		},
		link:function(s,e,a,c){
			e.on('click',function(){
				c.dohand();
			})
		}
	}
})
//关注
app.directive('dLove',function(){
	return {
		scope:{
			dLove:'='
		},
		controller:function($scope,$http,$stateParams,$rootScope){
			//牵手
			this.dolove=function(_uid){
				var _link=buildUrl('json','hn','doLove',{
					uid:(_uid?_uid:$stateParams.uid)
				})
				$rootScope.uiObj.layerloadingCtrl=true;
				$http.get(_link).success(function(data){
					$rootScope.callSuccessHttp();
					if(data.code==0)
					{
						$scope.dLove.data.loveIt=1;
						
					}
					else
					{
						
						if(data.code==98)
						{
							goreg('您还没有注册，是否需要前往注册页面？')
						}
						else if(data.code==97)
						{
							gorecharge('关注成功后将会花费一次配对次数，请先充值购买配对次数。')
						}
						else
						{
							$.util.alert(data.msg)
						}
						
					}
					
				})
			}
		},
		link:function(s,e,a,c){
			e.on('click',function(){
				var id=e.attr('uid');
				console.log(id);
				c.dolove(id);
			})
		}
	}
})

app.directive('showImgwx',function(){
	return {
		scope:{
			showImgwx:"="
		},
		controller:function($scope,$http,$stateParams){
			this.showimg=function(){
				var imgs=$scope.showImgwx.imgs,
					index=$scope.showImgwx.index;
				wx_share.showImg(imgs,index);
			}
		},
		link:function(s,e,a,c){
			e.on('click',function(){
				c.showimg();
			})
		}
	}
})

app.directive('hnBase',function(){
	return {
		controller:function($scope,$http,$rootScope){
			$rootScope.uiObj.layerloadingCtrl=true;
			var _link = buildUrl('json','hn','centerBase', {});
			$http.get(_link).success(function(response) {
				$scope.data=response;
				$rootScope.callSuccessHttp();
			})
		}
	}
})

//下拉加载分页
app.directive('sqDropmoreajax', function() {
	return {
		scope: {
			sqDropmoreajax: '='
		},
		controller: function($scope, $http, $rootScope) {
			var _this = this,
				dpa = $scope.sqDropmoreajax,
				start = 0,
				limit = parseInt(dpa.defaultLimit);
			console.log(dpa.data);
			$scope.isloaded = true;
			$scope.isWork = true;
			dpa.data.text = '下拉加载更多';

			this.getlist = function() {
				var _link = buildUrl(dpa.type, dpa.module, dpa.method, {
					start: start,
					limit: limit
				})

				if($scope.isWork && $scope.isloaded) {

					$scope.isWork = false;
					$scope.isloaded = false;
					dpa.data.text = '正在加载..';
					$http.get(_link).success(function(response) {
						if(response.code != 99) {
							if(start == 0) {
								dpa.data.data = response;
							} else {
								for(var i = 0; i < response.results.length; i++) {
									dpa.data.data.results.push(response.results[i]);
								}
							}
							if(!response.total||(start + limit) >= response.total) {
								$scope.isWork = false;
							} else {
								$scope.isWork = true;
								start = start + limit;
							}

						} else {
							$scope.isWork = false;
						}

						if($scope.isWork) {
							dpa.data.text = '下拉加载更多';
						} else {
							dpa.data.text = '没有更多的数据';
						}

						$scope.isloaded = true;
						$rootScope.callSuccessHttp();
					});
					
				}
			}

			dpa.data.dropData = _this.getlist;
		},
		link: function(s, e, a, c) {
			c.getlist();
			//监听滚动条
			window.onscroll = function() {　　
				if(getScrollTop() + getWindowHeight() == getScrollHeight() && getScrollTop() != 0) {　　　　
					c.getlist();
				}
			}
		}
	}
})
